package cn.xs.goods.service.impl;


import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import cn.xs.core.entity.PageResult;
import cn.xs.core.service.impl.BaseServiceImpl;
import cn.xs.goods.dao.CategoryDao;
import cn.xs.goods.dao.GoodDao;
import cn.xs.goods.entity.Category;
import cn.xs.goods.entity.Good;
import cn.xs.goods.service.GoodService;

@Service("goodService")
public class GoodServiceImpl extends BaseServiceImpl<Good> implements GoodService {

	@Resource
	private CategoryDao categoryDao;
	private GoodDao goodDao;
	@Resource
	public void setGoodDao(GoodDao goodDao) {
		super.setBaseDao(goodDao);
		this.goodDao = goodDao;
	}
	
	
	@Override
	public Good find(String id) {
		Good good = goodDao.find(id);
		//填充分类
		Category category = categoryDao.find(good.getCategory_id());
		good.setCategory(category);
		return good;
	}


	public List<Good> findAllByCategory(String categoryid) {
		return goodDao.findAllByCategory(categoryid);
	}


	public PageResult getPageResult(String categoryid, int pageNo, int pageSize) {
		List<Good> items = goodDao.findAllByCategoryAndLimit(categoryid,pageNo,pageSize);
		PageResult result = new PageResult();
		result.setItems(items);
		result.setPageNo(pageNo);
		result.setPageSize(pageSize);
		result.setTotalCount(goodDao.countGoodsByCategory(categoryid));
		return result;
	}


	public PageResult getPageResult(int pageNo, int pageSize, String searchName) {
		List<Good> items = goodDao.findAllBySearchAndLimit(searchName,pageNo,pageSize);
		PageResult result = new PageResult();
		result.setItems(items);
		result.setPageNo(pageNo);
		result.setPageSize(pageSize);
		return result;
	}
	
	
}
